/*
This file generates the variables used in the paper, from the raw data from EU-SILC.

Please change this path into your personal folder where the code files were placed. 
*/

	global read "folder/data/"


	cd "$read"

	*part 1 input raw files from EU-SILC 
		    *output separate files for each year: "UDB_L05D_ver year"
	*part 2 input separate files for each year: "UDB_L05D_ver year" 
			*output for each year: "UDB_id_transf_hrpd_year"
	*part 3 input for each year and individual and family files: "UDB_id_transf_hrpd_year" 
			*output longitudinal file "UDB_hrpd_2005_2016"
	*part 4 input "UDB_hrpd_2005_2016" preparation of main variables 
		    *output UDB_hrpd_2016_a_2005_tsset1
	*part 5 input UDB_hrpd_2016_a_2005_tsset1 main variables to be used
			*output dataset


************************Part1 *******************************************************************************************
*************************************************************************************************************************
clear all

set more off
local lletter "D H P R"
foreach ll of local lletter  {
local ys "05 06 07 08 09 10 11 12 13 14 15 16"
foreach y of local ys  {
local lcountries "AT BE BG CH CY CZ DK EE EL ES FI FR HR HU IE IS IT LT LU LV MT NL NO PL PT RO RS SE SI SK UK"
foreach lc of local lcountries {
clear all
cap import delimited "$read/SILC1995_2016/`lc'/20`y'/UDB_l`lc'`y'`ll'.csv", encoding(ISO-8859-1) clear
cap save "SILC_`lc'_`ll'_`y'.dta",replace
}  //lleter R P H D
} // ano 2016 ate 2005
} // coutries primeiro AT

*step 2
local lletter "D H P R"
foreach ll of local lletter  {
local ys "05 06 07 08 09 10 11 12 13 14 15 16"
foreach y of local ys  {
    local lcountries "BE BG CH CY CZ DK EE EL ES FI FR HR HU IE IS IT LT LU LV MT NL NO PL PT RO RS SE SI SK UK"
	use "SILC_AT_`ll'_`y'.dta", clear
	foreach lc of local lcountries {
	cap append using "SILC_`lc'_`ll'_`y'.dta"
	}  //lleter R P H D
	save "SILC_`ll'_`y'.dta", replace
} // ano 2016 ate 2005
} // coutries primeiro AT

local ys "05 06 07 08 11 12 13 14 15 16"
foreach y of local ys  {
	use "SILC_R_`y'.dta", clear
**************identificadores***********************

generate double Longitudinal_db=20`y'
generate double year=rb010
generate double pid=rb030
format pid  %20.0g
generate double hid=rb040
format hid  %20.0g

generate byte country=.
replace country=1 if rb020=="AT"
replace country=2 if rb020=="BE"
replace country=3 if rb020=="BG"
replace country=4 if rb020=="CY"
replace country=5 if rb020=="CZ"
replace country=6 if rb020=="DK"
replace country=7 if rb020=="EE"
replace country=8 if rb020=="ES"
replace country=9 if rb020=="FI" 
replace country=10 if rb020=="FR"
replace country=11 if rb020=="GR"|rb020=="EL"
replace country=12 if rb020=="HU"
replace country=13 if rb020=="IE"
replace country=14 if rb020=="IS"
replace country=15 if rb020=="IT"
replace country=16 if rb020=="LT"
replace country=17 if rb020=="LU"
replace country=18 if rb020=="LV"
replace country=19 if rb020=="NL"
replace country=20 if rb020=="NO" 
replace country=21 if rb020=="PL"
replace country=22 if rb020=="PT"
replace country=23 if rb020=="RO"
replace country=24 if rb020=="SE"
replace country=25 if rb020=="SI"
replace country=26 if rb020=="SK"
replace country=27 if rb020=="UK"
replace country=28 if rb020=="MT"
replace country=29 if rb020=="DE"
replace country=30 if rb020=="CH"
replace country=31 if rb020=="HR" 
replace country=32 if rb020=="RS"
compress

gen k = 0
egen string c_pid = concat(country k pid), format(%19.0g)
egen string c_hid = concat(country k hid), format(%19.0g)
egen string y_c_pid = concat(year k country k pid), format(%40.0g)
egen string y_c_hid = concat(year k country k hid), format(%40.0g)

save "UDB_l`y'R_ver 20`y'", replace


**********************************Ficheiro Dados dos indivÌduos**********************************************************
***********************************************************************************************************************
 
use "SILC_P_`y'.dta", clear
**************CriaÁ„o de identificadores***********************

generate double year=pb010
generate double pid=pb030
format pid  %12.0g

generate byte country=.
replace country=1 if pb020=="AT"
replace country=2 if pb020=="BE"
replace country=3 if pb020=="BG"
replace country=4 if pb020=="CY"
replace country=5 if pb020=="CZ"
replace country=6 if pb020=="DK"
replace country=7 if pb020=="EE"
replace country=8 if pb020=="ES"
replace country=9 if pb020=="FI" 
replace country=10 if pb020=="FR"
replace country=11 if pb020=="GR"|pb020=="EL"
replace country=12 if pb020=="HU"
replace country=13 if pb020=="IE"
replace country=14 if pb020=="IS"
replace country=15 if pb020=="IT"
replace country=16 if pb020=="LT"
replace country=17 if pb020=="LU"
replace country=18 if pb020=="LV"
replace country=19 if pb020=="NL"
replace country=20 if pb020=="NO" 
replace country=21 if pb020=="PL"
replace country=22 if pb020=="PT"
replace country=23 if pb020=="RO"
replace country=24 if pb020=="SE"
replace country=25 if pb020=="SI"
replace country=26 if pb020=="SK"
replace country=27 if pb020=="UK"
replace country=28 if pb020=="MT"
replace country=29 if pb020=="DE"
replace country=30 if pb020=="CH"
replace country=31 if pb020=="HR" 
replace country=32 if pb020=="RS"
compress

*concatenar paÌs com household id
gen k = 0
egen string c_pid = concat(country k pid), format(%19.0g)
egen string y_c_pid = concat(year k country k pid), format(%40.0g)

save "UDB_l`y'P_ver 20`y'",replace

**********************************Ficheiro Dados das FamÌlias**********************************************************
***********************************************************************************************************************

use "SILC_H_`y'.dta", clear
generate double year=hb010
generate double hid=hb030
format hid  %20.0g

generate byte country=.
replace country=1 if hb020=="AT"
replace country=2 if hb020=="BE"
replace country=3 if hb020=="BG"
replace country=4 if hb020=="CY"
replace country=5 if hb020=="CZ"
replace country=6 if hb020=="DK"
replace country=7 if hb020=="EE"
replace country=8 if hb020=="ES"
replace country=9 if hb020=="FI" 
replace country=10 if hb020=="FR"
replace country=11 if hb020=="GR"|hb020=="EL"
replace country=12 if hb020=="HU"
replace country=13 if hb020=="IE"
replace country=14 if hb020=="IS"
replace country=15 if hb020=="IT"
replace country=16 if hb020=="LT"
replace country=17 if hb020=="LU"
replace country=18 if hb020=="LV"
replace country=19 if hb020=="NL"
replace country=20 if hb020=="NO" 
replace country=21 if hb020=="PL"
replace country=22 if hb020=="PT"
replace country=23 if hb020=="RO"
replace country=24 if hb020=="SE"
replace country=25 if hb020=="SI"
replace country=26 if hb020=="SK"
replace country=27 if hb020=="UK"
replace country=28 if hb020=="MT"
replace country=29 if hb020=="DE"
replace country=30 if hb020=="CH"
replace country=31 if hb020=="HR" 
replace country=32 if hb020=="RS"
compress

gen k = 0
egen string c_hid = concat(country k hid), format(%19.0g)
egen string y_c_hid = concat(year k country k hid), format(%40.0g)

save "UDB_l`y'H_ver 20`y'.dta",replace

************************Ficheiro Registo das famÌlias*******************************************
****************************************************************************************************

use "SILC_D_`y'.dta", clear
generate double year=db010
generate double hid=db030
format hid  %20.0g

generate byte country=.
replace country=1 if db020=="AT"
replace country=2 if db020=="BE"
replace country=3 if db020=="BG"
replace country=4 if db020=="CY"
replace country=5 if db020=="CZ"
replace country=6 if db020=="DK"
replace country=7 if db020=="EE"
replace country=8 if db020=="ES"
replace country=9 if db020=="FI" 
replace country=10 if db020=="FR"
replace country=11 if db020=="GR"|db020=="EL"
replace country=12 if db020=="HU"
replace country=13 if db020=="IE"
replace country=14 if db020=="IS"
replace country=15 if db020=="IT"
replace country=16 if db020=="LT"
replace country=17 if db020=="LU"
replace country=18 if db020=="LV"
replace country=19 if db020=="NL"
replace country=20 if db020=="NO" 
replace country=21 if db020=="PL"
replace country=22 if db020=="PT"
replace country=23 if db020=="RO"
replace country=24 if db020=="SE"
replace country=25 if db020=="SI"
replace country=26 if db020=="SK"
replace country=27 if db020=="UK"
replace country=28 if db020=="MT"
replace country=29 if db020=="DE"
replace country=30 if db020=="CH"
replace country=31 if db020=="HR" 
replace country=32 if db020=="RS"
compress

gen k = 0
egen string c_hid = concat(country k hid), format(%19.0g)
egen string y_c_hid = concat(year k country k hid), format(%40.0g)
save "UDB_l`y'D_ver 20`y'",replace

}


local ys "09 10"
foreach y of local ys  {
	use "SILC_R_`y'.dta", clear

************************Ficheiro Registo dos indidivÌduos*******************************************
****************************************************************************************************

**************CriaÁ„o de identificadores***********************

generate double Longitudinal_db=20`y'
generate double year=rb010
generate double pid=rb030
format pid  %20.0g
generate double hid=rb040
format hid  %20.0g

generate byte country=.
replace country=1 if rb020=="AT"
replace country=2 if rb020=="BE"
replace country=3 if rb020=="BG"
replace country=4 if rb020=="CY"
replace country=5 if rb020=="CZ"
replace country=6 if rb020=="DK"
replace country=7 if rb020=="EE"
replace country=8 if rb020=="ES"
replace country=9 if rb020=="FI" 
replace country=10 if rb020=="FR"
replace country=11 if rb020=="GR"|rb020=="EL"
replace country=12 if rb020=="HU"
replace country=13 if rb020=="IE"
replace country=14 if rb020=="IS"
replace country=15 if rb020=="IT"
replace country=16 if rb020=="LT"
replace country=17 if rb020=="LU"
replace country=18 if rb020=="LV"
replace country=19 if rb020=="NL"
replace country=20 if rb020=="NO" 
replace country=21 if rb020=="PL"
replace country=22 if rb020=="PT"
replace country=23 if rb020=="RO"
replace country=24 if rb020=="SE"
replace country=25 if rb020=="SI"
replace country=26 if rb020=="SK"
replace country=27 if rb020=="UK"
replace country=28 if rb020=="MT"
replace country=29 if rb020=="DE"
replace country=30 if rb020=="CH"
replace country=31 if rb020=="HR" 
replace country=32 if rb020=="RS"
compress

gen k = 0
egen string c_pid = concat(country k pid), format(%19.0g)
egen string c_hid = concat(country k hid), format(%19.0g)
egen string y_c_pid = concat(year k country k pid), format(%40.0g)
egen string y_c_hid = concat(year k country k hid), format(%40.0g)

save "UDB_l`y'R_ver 20`y'", replace


**********************************Ficheiro Dados dos indivÌduos**********************************************************
***********************************************************************************************************************

*import delimited "/Users/pedroraposo/Documents/Dados/SILC/2009-4/UDB_l09P_ver 2009-4 from 01-03-2013.csv", encoding(ISO-8859-1)clear 
use "SILC_P_`y'.dta", clear 
**************CriaÁ„o de identificadores***********************

generate double year=pb010
generate double pid=pb030
format pid  %12.0g

generate byte country=.
replace country=1 if pb020=="AT"
replace country=2 if pb020=="BE"
replace country=3 if pb020=="BG"
replace country=4 if pb020=="CY"
replace country=5 if pb020=="CZ"
replace country=6 if pb020=="DK"
replace country=7 if pb020=="EE"
replace country=8 if pb020=="ES"
replace country=9 if pb020=="FI" 
replace country=10 if pb020=="FR"
replace country=11 if pb020=="GR"
replace country=12 if pb020=="HU"
replace country=13 if pb020=="IE"
replace country=14 if pb020=="IS"
replace country=15 if pb020=="IT"
replace country=16 if pb020=="LT"
replace country=17 if pb020=="LU"
replace country=18 if pb020=="LV"
replace country=19 if pb020=="NL"
replace country=20 if pb020=="NO" 
replace country=21 if pb020=="PL"
replace country=22 if pb020=="PT"
replace country=23 if pb020=="RO"
replace country=24 if pb020=="SE"
replace country=25 if pb020=="SI"
replace country=26 if pb020=="SK"
replace country=27 if pb020=="UK"
replace country=28 if pb020=="MT"
replace country=29 if pb020=="DE"
replace country=30 if pb020=="CH"
replace country=31 if pb020=="HR"
compress

gen k = 0
egen string c_pid = concat(country k pid), format(%19.0g)
egen string y_c_pid = concat(year k country k pid), format(%40.0g)

save "UDB_l`y'P_ver 20`y'",replace


**********************************Ficheiro Dados das FamÌlias**********************************************************
***********************************************************************************************************************

use "SILC_H_`y'.dta", clear 
generate double year=hb010
generate double hid=hb030
format hid  %20.0g

generate byte country=.
replace country=1 if hb020=="AT"
replace country=2 if hb020=="BE"
replace country=3 if hb020=="BG"
replace country=4 if hb020=="CY"
replace country=5 if hb020=="CZ"
replace country=6 if hb020=="DK"
replace country=7 if hb020=="EE"
replace country=8 if hb020=="ES"
replace country=9 if hb020=="FI" 
replace country=10 if hb020=="FR"
replace country=11 if hb020=="GR"
replace country=12 if hb020=="HU"
replace country=13 if hb020=="IE"
replace country=14 if hb020=="IS"
replace country=15 if hb020=="IT"
replace country=16 if hb020=="LT"
replace country=17 if hb020=="LU"
replace country=18 if hb020=="LV"
replace country=19 if hb020=="NL"
replace country=20 if hb020=="NO" 
replace country=21 if hb020=="PL"
replace country=22 if hb020=="PT"
replace country=23 if hb020=="RO"
replace country=24 if hb020=="SE"
replace country=25 if hb020=="SI"
replace country=26 if hb020=="SK"
replace country=27 if hb020=="UK"
replace country=28 if hb020=="MT"
replace country=29 if hb020=="DE"
replace country=30 if hb020=="CH"
replace country=31 if hb020=="HR"
compress

gen k = 0
egen string c_hid = concat(country k hid), format(%19.0g)

egen string y_c_hid = concat(year k country k hid), format(%40.0g)
save "UDB_l`y'H_ver 20`y'",replace

************************Ficheiro Registo das famÌlias*******************************************
****************************************************************************************************

use "SILC_D_`y'.dta", clear 
generate double year=db010
generate double hid=db030
format hid  %20.0g

generate byte country=.
replace country=1 if db020=="AT"
replace country=2 if db020=="BE"
replace country=3 if db020=="BG"
replace country=4 if db020=="CY"
replace country=5 if db020=="CZ"
replace country=6 if db020=="DK"
replace country=7 if db020=="EE"
replace country=8 if db020=="ES"
replace country=9 if db020=="FI" 
replace country=10 if db020=="FR"
replace country=11 if db020=="GR"
replace country=12 if db020=="HU"
replace country=13 if db020=="IE"
replace country=14 if db020=="IS"
replace country=15 if db020=="IT"
replace country=16 if db020=="LT"
replace country=17 if db020=="LU"
replace country=18 if db020=="LV"
replace country=19 if db020=="NL"
replace country=20 if db020=="NO" 
replace country=21 if db020=="PL"
replace country=22 if db020=="PT"
replace country=23 if db020=="RO"
replace country=24 if db020=="SE"
replace country=25 if db020=="SI"
replace country=26 if db020=="SK"
replace country=27 if db020=="UK"
replace country=28 if db020=="MT"
replace country=29 if db020=="DE"
replace country=30 if db020=="CH"
replace country=31 if db020=="HR"
compress

gen k = 0
egen string c_hid = concat(country k hid), format(%19.0g)

egen string y_c_hid = concat(year k country k hid), format(%40.0g)

save "UDB_l`y'D_ver 20`y'",replace
}

************************Part2 *******************************************************************************************
*************************************************************************************************************************

cap import delimited "deflators.csv", clear
save "Deflators.dta",replace

local ys "13 14 15 16"
foreach y of local ys  {

**********************************Ficheiro Dados das FamÌlias**********************************************************
***********************************************************************************************************************
set more off, perm

use "UDB_l`y'H_ver 20`y'", clear
capture noisily generate Tenure_Status_020=hh020
capture noisily generate Tenure_Status_021=hh021
drop *_f *_i
drop hs* hh* 

duplicates report c_hid year


******************* Deflacionar rendimento **********************************************

merge m:1 year country using "Deflators.dta", keep(match)


foreach var of varlist hy010 hy020 hy022 hy023  hy030n hy040n hy050n hy060n hy070n hy080n hy081n hy090n ///
hy100n hy110n hy120n hy130n hy131n hy140n hy145n hy030g hy040g hy050g hy060g hy070g hy080g hy081g hy090g hy100g ///
hy110g hy120g hy130g hy131g hy140g {
generate `var'_p2008=(`var'*(deflator/100)) /* if country_code!="SK" */  
}

drop hy010 hy020 hy022 hy023 hy030n hy040n hy050n hy060n hy070n hy080n hy081n hy090n ///
hy100n hy110n hy120n hy130n hy131n hy140n hy145n hy030g hy040g hy050g hy060g hy070g hy080g hy081g hy090g hy100g ///
hy110g hy120g hy130g hy131g hy140g 

drop _merge

****************** para juntar a informaÁ„o do ficheiro H  com a informaÁ„o do ficheiro R ******************

merge 	1:m year country hid using  "UDB_l`y'R_ver 20`y'"
generate _merge_hr_20`y'=_merge
drop _merge
drop if _merge_hr_20`y'==2 |_merge_hr_20`y'==1
sort y_c_pid

foreach var of varlist  rb060 rb062 rb063 rb064 {
generate `var'_20`y'=`var'
}
drop rb060 rb062 rb063 rb064

************************Ficheiro Registo dos indidivÌduos*******************************************
****************************************************************************************************
**************CriaÁ„o e eliminaÁ„o de vari·veis***********************
drop *_f 
*******IndivÌduos com mais de uma famÌlia no mesmo ano*************************

duplicates report country pid year
duplicates report country pid year hid
duplicates tag country pid year , generate(repeticoes)

************** Determinar Rendimento equivalente famÌlia***********************************
************** Determinar factores de equivalÍncia*****************************************
****************
generate age_r=year-rb080-1
keep  if age_r<.
keep  if rx020<.


****************** para juntar a informaÁ„o do ficheiro HR  com a informaÁ„o do ficheiro P ******************

merge m:1 year country pid using "UDB_l`y'P_ver 20`y'"
rename _merge _merge_hrp_20`y'
drop if _merge_hrp_20`y'==2 //if household does not have that individual

**********************************Ficheiro Dados dos indivÌduos**********************************************************
***********************************************************************************************************************
**************CriaÁ„o e eliminaÁ„o de vari·veis***********************
drop *_f *_i
generate NivelEducacional=pe040
cap generate current_sc_status30=pl030
generate current_sc_status31=pl031
generate General_health=ph010

drop pe* ph* 

generate age_p=year-pb140-1
sort y_c_pid

******************* Deflacionar rendimento pessoal**********************************************

*not in 2014 py070n py070g
foreach var of varlist    py010n py020n py035n py050n py080n py090n py100n py110n py120n py130n py140n ///
py010g py020g py035g py050g  py080g py090g py100g py110g py120g py130g py140g  py021n py021g {
generate `var'_p2008=(`var'*(deflator/100)) /* if country_code!="SK" */  
}

drop    py010n py020n py035n py050n py080n py090n py100n py110n py120n py130n py140n py010g py020g ///
py035g py050g py080g py090g py100g py110g py120g py130g py140g  py021n py021g
**********************************Ficheiro Registo das famÌlias**********************************************************
***********************************************************************************************************************

merge m:1 year country hid using "UDB_l`y'D_ver 20`y'.dta" 
generate _merge_hrpd_20`y'=_merge
drop _merge
drop if _merge_hrpd_20`y'==2
save "UDB_id_transf_hrpd_20`y'",replace

*******************************************************************************************************

**************************************************** ver indivÌduos duplicados para o mesmo ano : mais do que uma famÌlia**************
drop repeticoes
duplicates report country pid year hid
duplicates tag country pid year , generate(repeticoes)

sort  hid y_c_pid

rename rb140 Month_out
rename rb150 Year_out
rename rb160 N_Months_Inc_Per
rename rb180 Month_in
rename rb190 Year_in

foreach var of varlist hy010_p2008 hy020_p2008 hy022_p2008 hy023_p2008  hy030n_p2008 ///
hy040n_p2008 hy050n_p2008 hy060n_p2008 hy070n_p2008 hy080n_p2008 hy081n_p2008 hy090n_p2008 ///
hy100n_p2008 hy110n_p2008 hy120n_p2008 hy130n_p2008 hy131n_p2008 hy140n_p2008 hy145n_p2008 ///
hy030g_p2008 hy040g_p2008 hy050g_p2008 hy060g_p2008 hy070g_p2008 hy080g_p2008 hy081g_p2008 ///
hy090g_p2008 hy100g_p2008 ///
hy110g_p2008 hy120g_p2008 hy130g_p2008 hy131g_p2008 hy140g_p2008 ///
  py010n_p2008 py020n_p2008 py035n_p2008 py050n_p2008 py080n_p2008 py090n_p2008 py100n_p2008 py110n_p2008 py120n_p2008 py130n_p2008 py140n_p2008 py010g_p2008 ///
  py020g_p2008 py035g_p2008 py050g_p2008 py080g_p2008 py090g_p2008 py100g_p2008 py110g_p2008 py120g_p2008 py130g_p2008 py140g_p2008  py021n_p2008 py021g_p2008 {
bysort y_c_pid: egen `var'_m= mean(`var')  
}

drop hy010_p2008 hy020_p2008 hy022_p2008 hy023_p2008  hy030n_p2008 ///
hy040n_p2008 hy050n_p2008 hy060n_p2008 hy070n_p2008 hy080n_p2008 hy081n_p2008 hy090n_p2008 ///
hy100n_p2008 hy110n_p2008 hy120n_p2008 hy130n_p2008 hy131n_p2008 hy140n_p2008 hy145n_p2008 ///
hy030g_p2008 hy040g_p2008 hy050g_p2008 hy060g_p2008 hy070g_p2008 hy080g_p2008 hy081g_p2008 ///
hy090g_p2008 hy100g_p2008 ///
hy110g_p2008 hy120g_p2008 hy130g_p2008 hy131g_p2008 hy140g_p2008 ///
py010n_p2008 py020n_p2008 py035n_p2008 py050n_p2008 py080n_p2008 py090n_p2008 py100n_p2008 py110n_p2008 ///
py120n_p2008 py130n_p2008 py140n_p2008 py010g_p2008 ///
py020g_p2008 py035g_p2008 py050g_p2008 py080g_p2008 py090g_p2008 py100g_p2008 py110g_p2008 py120g_p2008 py130g_p2008 py140g_p2008 ///
py021n_p2008 py021g_p2008


drop  if repeticoes==1 & (rb110==5|rb110==6|rb110==7)
duplicates tag country pid year , generate(repeticoes2)
generate hid2=-hid
sort y_c_pid -hid2

drop repeticoes repeticoes2 hid2

save "UDB_id_transf_hrpd_20`y'",replace

}

local ys "05 06 07 08 09 10 11 12"
foreach y of local ys  {

use "UDB_L`y'H_ver 20`y'.dta", clear
capture noisily generate Tenure_Status_020=hh020
capture noisily generate Tenure_Status_021=hh021
drop *_f *_i
drop hs* hh* 

duplicates report c_hid year

******************* Deflacionar rendimento **********************************************

merge m:1 year country using "Deflators.dta", keep(match)


foreach var of varlist hy010 hy020 hy022 hy023  hy030n hy040n hy050n hy060n hy070n hy080n  hy090n ///
hy100n hy110n hy120n hy130n  hy140n hy145n hy030g hy040g hy050g hy060g hy070g hy080g  hy090g hy100g ///
hy110g hy120g hy130g  hy140g {
generate `var'_p2008=(`var'*(deflator/100)) 
}


drop hy010 hy020  hy022 hy023   hy030n hy040n hy050n hy060n hy070n hy080n  hy090n ///
hy100n hy110n hy120n hy130n  hy140n hy145n hy030g hy040g hy050g hy060g hy070g hy080g  hy090g hy100g ///
hy110g hy120g hy130g  hy140g 

drop _merge

****************** para juntar a informaÁ„o do ficheiro H  com a informaÁ„o do ficheiro R ******************

* merge deixando cair indivÌduos que est„o no ficheiro R com famÌlia (para um dado ano)
* mas no ficheiro H n„o tem famÌlia para esse ano, logo n„o tem rendimento 
merge 	1:m year country hid using  "UDB_L`y'R_ver 20`y'.dta"
generate _merge_hr_20`y'=_merge
drop _merge
drop if _merge_hr_20`y'==2|_merge_hr_20`y'==1
sort y_c_pid
* Base de dados longitudinal de 2005 n„o tem rb064, rb063  e rb062(!)
if `y'==05 {
	foreach var of varlist  rb060 {
generate `var'_20`y'=`var'
}
drop rb060
}

if `y'==06 {
	foreach var of varlist  rb060 rb062 rb063 {
generate `var'_20`y'=`var'
}
drop rb060 rb062 rb063
}
if `y'!=05&`y'!=06 {
	foreach var of varlist  rb060 rb062 rb063 rb064 {
generate `var'_20`y'=`var'
}
drop rb060 rb062 rb063 rb064
}

************************Ficheiro Registo dos indidivÌduos*******************************************
****************************************************************************************************
**************CriaÁ„o e eliminaÁ„o de vari·veis***********************
drop *_f 
*******IndivÌduos com mais de uma famÌlia no mesmo ano*************************
duplicates report country pid year
duplicates report country pid year hid
duplicates tag country pid year , generate(repeticoes)

************** Determinar Rendimento equivalente famÌlia***********************************
************** Determinar factores de equivalÍncia*****************************************

generate age_r=year-rb080-1

keep  if age_r<.
keep  if rx020<.

****************** para juntar a informaÁ„o do ficheiro HR  com a informaÁ„o do ficheiro P ******************

merge m:1 year country pid using "UDB_L`y'P_ver 20`y'.dta"
rename _merge _merge_hrp_20`y'
drop if _merge_hrp_20`y'==2

**********************************Ficheiro Dados dos indivÌduos**********************************************************
***********************************************************************************************************************
**************CriaÁ„o e eliminaÁ„o de vari·veis***********************
drop *_f *_i
generate NivelEducacional=pe040
generate current_sc_status30=pl030
generate General_health=ph010
drop pe* ph*  

generate age_p=year-pb140-1

sort y_c_pid


******************* Deflacionar rendimento pessoal**********************************************


foreach var of varlist  py010n py020n py035n py050n py070n py080n py090n py100n py110n py120n py130n py140n py010g py020g  ///
 py035g py050g py070g py080g py090g py100g py110g py120g py130g py140g {
generate `var'_p2008=(`var'*(deflator/100)) 
}


drop  py010n py020n  py035n py050n py070n py080n py090n py100n py110n py120n py130n py140n py010g py020g  ///
 py035g py050g py070g py080g py090g py100g py110g py120g py130g py140g
 
**********************************Ficheiro Registo das famÌlias**********************************************************
***********************************************************************************************************************

merge m:1 year country hid using "UDB_L`y'D_ver 20`y'.dta" 
generate _merge_hrpd_2009=_merge
drop _merge
drop if _merge_hrpd_2009==2
save "UDB_id_transf_hrpd_20`y'",replace
**************************************************************************

**************************************************** ver indivÌduos duplicados para o mesmo ano : mais do que uma famÌlia**************
drop repeticoes

duplicates report country pid year hid
duplicates tag country pid year , generate(repeticoes)

sort  hid y_c_pid

rename rb140 Month_out
rename rb150 Year_out
rename rb160 N_Months_Inc_Per
rename rb180 Month_in
rename rb190 Year_in


foreach var of varlist hy010_p2008 hy020_p2008 hy022_p2008 hy023_p2008  hy030n_p2008 ///
hy040n_p2008 hy050n_p2008 hy060n_p2008 hy070n_p2008 hy080n_p2008  hy090n_p2008 ///
hy100n_p2008 hy110n_p2008 hy120n_p2008 hy130n_p2008  hy140n_p2008 hy145n_p2008 ///
hy030g_p2008 hy040g_p2008 hy050g_p2008 hy060g_p2008 hy070g_p2008 hy080g_p2008  ///
hy090g_p2008 hy100g_p2008 ///
hy110g_p2008 hy120g_p2008 hy130g_p2008  hy140g_p2008 ///
 py010n_p2008 py020n_p2008 /*py021n_p2008*/ py035n_p2008 py050n_p2008 py070n_p2008 py080n_p2008 ///
 py090n_p2008 py100n_p2008 py110n_p2008 py120n_p2008 py130n_p2008 py140n_p2008 py010g_p2008 ///
 py020g_p2008 /*py021g_p2008*/ ///
 py035g_p2008 py050g_p2008 py070g_p2008 py080g_p2008 py090g_p2008 py100g_p2008 py110g_p2008 ///
 py120g_p2008 py130g_p2008 py140g_p2008 {
bysort y_c_pid: egen `var'_m= mean(`var')  
}

drop hy010_p2008 hy020_p2008 hy022_p2008 hy023_p2008  hy030n_p2008 ///
hy040n_p2008 hy050n_p2008 hy060n_p2008 hy070n_p2008 hy080n_p2008  hy090n_p2008 ///
hy100n_p2008 hy110n_p2008 hy120n_p2008 hy130n_p2008  hy140n_p2008 hy145n_p2008 ///
hy030g_p2008 hy040g_p2008 hy050g_p2008 hy060g_p2008 hy070g_p2008 hy080g_p2008  ///
hy090g_p2008 hy100g_p2008 ///
hy110g_p2008 hy120g_p2008 hy130g_p2008  hy140g_p2008 ///
 py010n_p2008 py020n_p2008  py035n_p2008 py050n_p2008 py070n_p2008 py080n_p2008 py090n_p2008 py100n_p2008 py110n_p2008 ///
 py120n_p2008 py130n_p2008 py140n_p2008 py010g_p2008 py020g_p2008  ///
 py035g_p2008 py050g_p2008 py070g_p2008 py080g_p2008 py090g_p2008 py100g_p2008 py110g_p2008 py120g_p2008 py130g_p2008 py140g_p2008

drop  if repeticoes==1 & (rb110==5|rb110==6|rb110==7)
duplicates tag country pid year , generate(repeticoes2)
generate hid2=-hid
sort y_c_pid -hid2

drop repeticoes repeticoes2 hid2

save "UDB_id_transf_hrpd_20`y'",replace
}


************************Part3 *******************************************************************************************
*************************************************************************************************************************

use "UDB_id_transf_hrpd_2010", clear
drop if year<2007
save "UDB_id_transf_hrpd_2010", replace
use "UDB_id_transf_hrpd_2009", clear
drop if year<2006
save "UDB_id_transf_hrpd_2009", replace
use "UDB_id_transf_hrpd_2008", clear
drop if year<2005
save "UDB_id_transf_hrpd_2008", replace
use "UDB_id_transf_hrpd_2007", clear
drop if year<2004
save "UDB_id_transf_hrpd_2007", replace

use "UDB_id_transf_hrpd_2016", clear

append using "UDB_id_transf_hrpd_2015" /*,update??? */
append using "UDB_id_transf_hrpd_2014" /*,update??? */
append using "UDB_id_transf_hrpd_2013" /*,update??? */
append using "UDB_id_transf_hrpd_2012" 
append using "UDB_id_transf_hrpd_2011", force 
append using "UDB_id_transf_hrpd_2010", force
append using "UDB_id_transf_hrpd_2009"
append using "UDB_id_transf_hrpd_2008"
append using "UDB_id_transf_hrpd_2007"
append using "UDB_id_transf_hrpd_2006"
append using "UDB_id_transf_hrpd_2005"

save "UDB_hrpd_2005_2016",replace


************************Part4 *******************************************************************************************
*************************************************************************************************************************

set more off

use "UDB_hrpd_2005_2016", clear

drop if hy020_p2008_m>=.

drop _merge_*

duplicates tag country pid year , generate(repeticoes)

generate Longitudinal_db_aux=-Longitudinal_db 
sort     y_c_pid Longitudinal_db_aux hid

foreach var of varlist hy010_p2008_m hy020_p2008_m hy022_p2008_m hy023_p2008_m  hy030n_p2008_m ///
hy040n_p2008_m hy050n_p2008_m hy060n_p2008_m hy070n_p2008_m hy080n_p2008_m  hy090n_p2008_m ///
hy100n_p2008_m hy110n_p2008_m hy120n_p2008_m hy130n_p2008_m  hy140n_p2008_m hy145n_p2008_m ///
hy030g_p2008_m hy040g_p2008_m hy050g_p2008_m hy060g_p2008_m hy070g_p2008_m hy080g_p2008_m  ///
hy090g_p2008_m hy100g_p2008_m ///
hy110g_p2008_m hy120g_p2008_m hy130g_p2008_m  hy140g_p2008_m ///
py010n_p2008_m py020n_p2008_m py035n_p2008_m py050n_p2008_m py070n_p2008_m py080n_p2008_m py090n_p2008_m ///
py100n_p2008_m py110n_p2008_m py120n_p2008_m py130n_p2008_m py140n_p2008_m py010g_p2008_m ///
py020g_p2008_m py035g_p2008_m py050g_p2008_m py070g_p2008_m py080g_p2008_m py090g_p2008_m py100g_p2008_m ///
py110g_p2008_m py120g_p2008_m py130g_p2008_m py140g_p2008_m {
bysort y_c_pid: egen `var'4= mean(`var')  
}

foreach var of varlist  hy081n_p2008_m hy131n_p2008_m hy081g_p2008_m hy131g_p2008_m {
bysort y_c_pid: egen `var'4= mean(`var')  
}


foreach var of varlist py021g_p2008_m py021n_p2008_m {
bysort y_c_pid: egen `var'4= mean(`var')  
}

drop hy010_p2008_m hy020_p2008_m hy022_p2008_m hy023_p2008_m  hy030n_p2008_m ///
hy040n_p2008_m hy050n_p2008_m hy060n_p2008_m hy070n_p2008_m hy080n_p2008_m  hy090n_p2008_m ///
hy100n_p2008_m hy110n_p2008_m hy120n_p2008_m hy130n_p2008_m  hy140n_p2008_m hy145n_p2008_m ///
hy030g_p2008_m hy040g_p2008_m hy050g_p2008_m hy060g_p2008_m hy070g_p2008_m hy080g_p2008_m  ///
hy090g_p2008_m hy100g_p2008_m ///
hy110g_p2008_m hy120g_p2008_m hy130g_p2008_m  hy140g_p2008_m 

drop   hy081n_p2008_m hy131n_p2008_m hy081g_p2008_m hy131g_p2008_m
drop py021g_p2008_m py021n_p2008_m

drop py010n_p2008_m py020n_p2008_m py035n_p2008_m py050n_p2008_m py070n_p2008_m py080n_p2008_m ///
py090n_p2008_m py100n_p2008_m py110n_p2008_m py120n_p2008_m py130n_p2008_m py140n_p2008_m py010g_p2008_m ///
py020g_p2008_m py035g_p2008_m py050g_p2008_m py070g_p2008_m py080g_p2008_m py090g_p2008_m py100g_p2008_m ///
py110g_p2008_m py120g_p2008_m py130g_p2008_m py140g_p2008_m


rename *p2008_m4 *p2008

drop  if repeticoes==1 & (rb110==5|rb110==6|rb110==7)

duplicates tag country pid year , generate(repeticoes2)

duplicates drop y_c_pid if repeticoes2==1, force 

drop repeticoes repeticoes2  Longitudinal_db_aux

drop k

drop c_pid

duplicates report year country pid

gen k = 0
egen string c_pid = concat(country k pid), format(%60.0g)
generate c_pid_n=c_pid
destring c_pid_n, replace 
format c_pid_n %60.0g
sort c_pid

bysort c_pid_n: egen Longitudinal_dbmax=max(Longitudinal_db)

keep if Longitudinal_dbmax==Longitudinal_db

tsset c_pid_n year
sort c_pid_n year, stable

drop k

drop pb010 pb020 pb030 pb100 pb110 pb120 pb130 pb140 pb150 pb160 pb170 pb180 /*pb190*/ pb200 

drop hb050 hb060 /*hb070*/ hb080 hb090 hb100 

generate hh_head=0
replace hh_head=1 if pid==hb070

drop rb200 rb210 rb220 rb230 rb240 rb245 rb250 rb260 rb270 

sort c_pid_n year, stable
tsset c_pid_n year
generate rx020lag1=L.rx020
generate rx020change=D.rx020 
generate rx020changelead1=F.rx020change 
generate rx020lead1=F.rx020
generate rx020prob=rx020-rx020lag1 if rx020<. & rx020lag1<.
generate rx020prob2=rx020prob if rx020prob!=1
generate rx020prob3=rx020prob2 if rx020prob!=0 | rx020<80
generate rx020prob3b=rx020prob3 if !inlist(country_code,"UK","IE") | !inlist(rx020prob3,0,2)
generate hidlag1=L.hid

generate rx020prob4=0 
replace rx020prob4=1 if rx020prob3b<.
bysort y_c_hid: egen rx020prob5=count(pid) if rx020prob4==1
tab rx020prob5
bysort y_c_hid: egen rx020prob6=max(rx020prob5)
replace rx020prob6=0 if rx020prob6>=.

generate rx020prob7=rx020prob6
replace rx020prob7=0 if rx020prob6>=.
replace rx020prob7=1 if rx020prob6>=1 & rx020prob6<.
drop if rx020prob7==1


bysort y_c_hid: egen deads0=count(pid) if year>Year_out
bysort y_c_hid: egen deads1=max(deads0)
replace deads1=0 if deads1>=.

generate deads2=deads1
replace deads2=0 if deads1>=.
replace deads2=1 if deads1>=1 & deads1<.
label define deadsb 0 "No deads" 1 "At least one dead" , replace
label values deads2 deads2

bysort y_c_hid: egen deadsb0=count(pid) if year<=Year_out & Year_out<.
bysort y_c_hid: egen deadsb1=max(deadsb0)
replace deadsb1=0 if deadsb1>=.

generate deadsb2=deadsb1
replace deadsb2=0 if deadsb1>=.
replace deadsb2=1 if deadsb1>=1 & deadsb1<.
label define deadsbb 0 "No deads b" 1 "At least one dead b" , replace
label values deadsb2 deadsb2

generate aux1=0
replace aux1=1 if rx020>=14 & rx020<.  & year<=Year_out /*incluidos aqueles que Year_out=. */

generate aux2=0 
replace aux2=1 if rx020 <14 & rx020>=0 & year<=Year_out /*incluidos aqueles que Year_out=. */ /* n„o inclui rx020==-1*/

bysort y_c_hid: egen n_adults=sum(aux1) 
bysort y_c_hid: egen n_childs=sum(aux2) /* n„o inclui rx020==-1*/
generate h_size_equivalized=1+(n_adults-1)/2+0.3*n_childs /*  fica missing se year<=Year_out*/
drop aux*
label var h_size_equivalized "Household size equivalized"

drop if h_size_equivalized <1

generate aux1=0 
replace aux1=1 if rx020>=14 & rx020<. & Year_out>=.
generate aux2=0 
replace aux2=1 if rx020 <14  & Year_out>=.
bysort y_c_hid: egen n_adults2=sum(aux1) 
bysort y_c_hid: egen n_childs2=sum(aux2) 
generate h_size_equivalized2=1+(n_adults2-1)/2+0.3*n_childs2
drop aux*
label var h_size_equivalized2 "Household size equivalized2"

generate  difhx050=h_size_equivalized-hx050

generate  difhx0502=h_size_equivalized2-hx050

generate hx050prob0=1 if h_size_equivalized2!=hx050 & difhx0502<. /* n„o inclui aqueles em que h_size_equivalized2 È missing*/

bysort y_c_hid: egen hx050prob1=max(hx050prob0)

generate aux1=0
replace aux1=1 if rx020>=14 & rx020<.  & rx020<65 & year<=Year_out /*incluidos aqueles que Year_out=. */
bysort y_c_hid: egen n_adultsless65=sum(aux1)
drop aux1 

drop if hx050prob1==1

* Atribuir rendimento equivalente a cada indivÌduo combinar dataset R com dataset H mas atenÁ„o aos pid com v·rias famÌlias
foreach var of varlist  hy010_p2008 hy020_p2008 hy022_p2008 hy023_p2008 hy030n_p2008 hy040n_p2008 hy050n_p2008 ///
hy060n_p2008 hy070n_p2008 hy080n_p2008 hy090n_p2008 hy100n_p2008 hy110n_p2008 hy120n_p2008 hy130n_p2008 ///
 hy140n_p2008 hy145n_p2008 hy030g_p2008 hy040g_p2008 hy050g_p2008 hy060g_p2008 hy070g_p2008 ///
hy080g_p2008  hy090g_p2008 hy100g_p2008 hy110g_p2008 hy120g_p2008 hy130g_p2008 ///
hy140g_p2008 ///
hy081n_p2008 hy131n_p2008 hy081g_p2008 hy131g_p2008 {
generate `var'_e=(`var'*hy025)/h_size_equivalized
}

foreach var of varlist  /* g's*/ py010g_p2008  py020g_p2008 /*py021g_p2008*/ py035g_p2008 py050g_p2008 ///
py070g_p2008 py080g_p2008 py090g_p2008 py100g_p2008 py110g_p2008 py120g_p2008 py130g_p2008 py140g_p2008 ///
/* n's*/ py010n_p2008  py020n_p2008 /*py021n_p2008*/ py035n_p2008 py050n_p2008 ///
py070n_p2008 py080n_p2008 py090n_p2008 py100n_p2008 py110n_p2008 py120n_p2008 py130n_p2008 py140n_p2008 {
generate `var'_e=(`var'*hy025)/h_size_equivalized
}

foreach var of varlist  py021g_p2008 py021n_p2008  {
generate `var'_e=(`var'*hy025)/h_size_equivalized
} 

drop /* hy010_p2008 hy020_p2008  hy022_p2008 hy023_p2008  */ hy030n_p2008 hy040n_p2008 hy050n_p2008 ///
hy060n_p2008 hy070n_p2008 hy080n_p2008  hy090n_p2008 hy100n_p2008 hy110n_p2008 hy120n_p2008 hy130n_p2008 ///
 hy140n_p2008 hy145n_p2008 hy030g_p2008 hy040g_p2008 hy050g_p2008 hy060g_p2008 hy070g_p2008 ///
hy080g_p2008  hy090g_p2008 hy100g_p2008 hy110g_p2008 hy120g_p2008 hy130g_p2008 ///
hy140g_p2008 hy081n_p2008 hy131n_p2008 hy081g_p2008 hy131g_p2008


drop py010g_p2008  py020g_p2008 py021g_p2008 py035g_p2008 py050g_p2008 ///
py070g_p2008 py080g_p2008 py090g_p2008 py100g_p2008 py110g_p2008 py120g_p2008 py130g_p2008 py140g_p2008 ///
py010n_p2008  py020n_p2008 py021n_p2008 py035n_p2008 py050n_p2008 ///
py070n_p2008 py080n_p2008 py090n_p2008 py100n_p2008 py110n_p2008 py120n_p2008 py130n_p2008 py140n_p2008

bysort y_c_hid: egen birth0=count(pid) if rx020==0 

bysort y_c_hid: egen birth1=max(birth0)
replace birth1=0 if birth1>=.

generate birth2=birth1
replace birth2=0 if birth1>=.
replace birth2=1 if birth1>=1 & birth1<.
label define birth 0 "No births" 1 "At least one birth" , replace
label values birth2 birth2
tab birth2

bysort y_c_hid: egen birthb0=count(pid) if rx020==-1

bysort y_c_hid: egen birthb1=max(birthb0)
replace birthb1=0 if birthb1>=.

generate birthb2=birthb1
replace birthb2=0 if birthb1>=.
replace birthb2=1 if birthb1>=1 & birthb1<.
label define birthb 0 "No births b" 1 "At least one birth b" , replace
label values birthb2 birthb2
tab birthb2

sort c_pid_n year, stable
tsset c_pid_n year
generate birth2lead1 = F.birth2
generate  Var_hy020_p2008_e=(d. hy020_p2008_e/L. hy020_p2008_e)
generate Var_hy020_p2008_elead1=F.Var_hy020_p2008_e
generate  Var3_hy020_p2008_e=((hy020_p2008_e-L3. hy020_p2008_e)/L3. hy020_p2008_e)/**100*/


sort c_pid_n year, stable
tsset c_pid_n year
generate hy020_p2008_elag1 = L.hy020_p2008_e

generate hy020_p2008_elead1 = F.hy020_p2008_e
generate  Var_hy020_p2008_eb=(d. hy020_p2008_e/L. hy020_p2008_e) if hy020_p2008_elag1<. & hy020_p2008_e<. & hy020_p2008_elag1>0 & hy020_p2008_e>0
generate Var_hy020_p2008_eblead1=F.Var_hy020_p2008_eb


*gerar vari·vel rendimento desfasada (t-1,t-2 e t-3) para c·lculo das taxas de variaÁ„o
generate L1hy020_p2008_e=L.hy020_p2008_e
generate L2hy020_p2008_e=L2.hy020_p2008_e
generate L3hy020_p2008_e=L3.hy020_p2008_e

generate country_euro=0
replace country_euro=1 if country_code=="AT" | country_code=="BE" | country_code=="ES" | country_code=="FI" | ///
country_code=="FR" | country_code=="GR" | country_code=="IE" | country_code=="IT" | country_code=="LU" | country_code=="NL" ///
| country_code=="PT" | country_code=="DE"  /* 12 paÌses */

replace country_euro=2 if country_code=="BG" | country_code=="CZ" | country_code=="DK" | country_code=="EE" | ///
country_code=="HU" | country_code=="IS" | country_code=="LT" | country_code=="NO" | country_code=="PL" ///
| country_code=="RO" | country_code=="SE" /* 11 paÌses */

generate hy020_p2008_e_mn=hy020_p2008_e*hx010 if country_euro==1

replace hy020_p2008_e_mn=hy020_p2008_e*hx010 if country_euro==2 

replace hy020_p2008_e_mn=hy020_p2008_e*hx010 if country_code=="UK" | country_code=="LV" /* UK + LV */

replace hy020_p2008_e_mn=hy020_p2008_e if country_code=="SI" | country_code=="MT" 

replace hy020_p2008_e_mn=hy020_p2008_e if country_code=="SK" |country_code=="CY" 

sort c_pid_n year, stable
generate L1hy020_p2008_e_mn=L.hy020_p2008_e_mn
generate L2hy020_p2008_e_mn=L2.hy020_p2008_e_mn
generate L3hy020_p2008_e_mn=L3.hy020_p2008_e_mn

generate  Var_hy020_p2008_e_mn=(d. hy020_p2008_e_mn/L. hy020_p2008_e_mn)*100
generate  Var3_hy020_p2008_e_mn=((hy020_p2008_e_mn-L3. hy020_p2008_e_mn)/L3. hy020_p2008_e_mn)*100


/* Persons charateristics: Age group*/

generate age_group2=1 if rx020>=16 & rx020<25
replace age_group2=2 if rx020>=25 & rx020<35
replace age_group2=3 if rx020>=35 & rx020<45
replace age_group2=4 if rx020>=45 & rx020<55
replace age_group2=5 if rx020>=55 & rx020<65
replace age_group2=6 if rx020>=65
label define age_group2 1 "16-24" 2 "25-34" 3 "35-44" 4 "45-54" 5 "55-64" 6 "65 ou mais",replace
label values age_group2 age_group2

generate hid_age_group1_0=1  if rx020>=16 & rx020<25
generate hid_age_group2_0=1  if rx020>=25 & rx020<35
generate hid_age_group3_0=1  if rx020>=35 & rx020<45
generate hid_age_group4_0=1  if rx020>=45 & rx020<55
generate hid_age_group5_0=1  if rx020>=55 & rx020<65
generate hid_age_group6_0=1  if rx020>=65

bysort y_c_hid: egen hid_age_group1=total(hid_age_group1_0)
bysort y_c_hid: egen hid_age_group2=total(hid_age_group2_0)
bysort y_c_hid: egen hid_age_group3=total(hid_age_group3_0)
bysort y_c_hid: egen hid_age_group4=total(hid_age_group4_0)
bysort y_c_hid: egen hid_age_group5=total(hid_age_group5_0)
bysort y_c_hid: egen hid_age_group6=total(hid_age_group6_0)

drop hid_age_group1_0 hid_age_group2_0 hid_age_group3_0 hid_age_group4_0 hid_age_group5_0 hid_age_group6_0
generate hid_age_group123456=hid_age_group1+hid_age_group2+hid_age_group3+hid_age_group4+hid_age_group5+hid_age_group6

generate hid_age_group1share=hid_age_group1/hid_age_group123456
generate hid_age_group2share=hid_age_group2/hid_age_group123456
generate hid_age_group3share=hid_age_group3/hid_age_group123456
generate hid_age_group4share=hid_age_group4/hid_age_group123456
generate hid_age_group5share=hid_age_group5/hid_age_group123456
generate hid_age_group6share=hid_age_group6/hid_age_group123456

/* Persons charateristics: Education group*/

generate NivEdu_group=1 if NivelEducacional==0|NivelEducacional==1|NivelEducacional==2
replace NivEdu_group=2 if NivelEducacional==3|NivelEducacional==4
replace NivEdu_group=3 if NivelEducacional==5|NivelEducacional==6
label define NivEdu_group 1 "PrÈ-prim·rio, prim·rio e secund·rio inferior" 2 "Secund·rio e pÛs-secund·rio" 3 "Terci·rio", replace
label values NivEdu_group NivEdu_group

generate hid_NivEdu_group1_0=1  if (NivelEducacional==0|NivelEducacional==1|NivelEducacional==2) & rx020>=16
generate hid_NivEdu_group2_0=1  if (NivelEducacional==3|NivelEducacional==4) & rx020>=16
generate hid_NivEdu_group3_0=1  if (NivelEducacional==5|NivelEducacional==6) & rx020>=16

bysort y_c_hid: egen hid_NivEdu_group1=total(hid_NivEdu_group1_0)  
bysort y_c_hid: egen hid_NivEdu_group2=total(hid_NivEdu_group2_0)  
bysort y_c_hid: egen hid_NivEdu_group3=total(hid_NivEdu_group3_0)  

drop hid_NivEdu_group1_0 hid_NivEdu_group2_0 hid_NivEdu_group3_0
generate hid_NivEdu_group123=hid_NivEdu_group1+hid_NivEdu_group2+hid_NivEdu_group3

generate hid_NivEdu_group1share=hid_NivEdu_group1/hid_NivEdu_group123
generate hid_NivEdu_group2share=hid_NivEdu_group2/hid_NivEdu_group123
generate hid_NivEdu_group3share=hid_NivEdu_group3/hid_NivEdu_group123

/* Persons charateristics: working age de 19 a 64*/

generate Workingagepersons=0
replace Workingagepersons=1 if rx020>15 & rx020<65
label define Workingagepersons  0 "Non working age person ate 15 e mais de 64" 1 "Working age person 16 a 64", replace
label values Workingagepersons  Workingagepersons 

generate Workingagepersons_plusmorethan64=0
replace Workingagepersons_plusmorethan64=1 if rx020>15 
label define Workingagepersons_plusmorethan64  0 "Non working age person ate 15 " 1 "Working age person 16 a 64 e 65 ou mais", replace
label values Workingagepersons_plusmorethan64  Workingagepersons_plusmorethan64 


generate Workingagepersons_restricted=0
replace Workingagepersons_restricted=1 if rx020>24 & rx020<60

local name1 pl210
local name2 pl211
local months "a b c d e f g h i j k l"
local varlist_temp1 "pl210a pl210b pl210c pl210d pl210e pl210f pl210g pl210h pl210i pl210j pl210k pl210l"
local varlist_temp2 "pl211a pl211b pl211c pl211d pl211e pl211f pl211g pl211h pl211i pl211j pl211k pl211l"

foreach varname of local varlist_temp2 {
generate `varname'_new=`varname'  if `varname'<.
replace `varname'_new=6 if `varname'==7 
replace `varname'_new=7 if `varname'==6
replace `varname'_new=8 if `varname'==10 | `varname'==11
}

foreach varname of local varlist_temp1 {
generate `varname'_new=`varname' if `varname'<. 
}

local name1 pl210
local name2 pl211
local months "a b c d e f g h i j k l"
foreach month of local months {
replace `name1'`month'_new=`name2'`month'_new if `name1'`month'_new>=. & `name2'`month'_new<.
}

drop  pl211a_new pl211b_new pl211c_new pl211d_new pl211e_new pl211f_new pl211g_new pl211h_new pl211i_new pl211j_new pl211k_new pl211l_new


local varlist_temp1new "pl210a_new pl210b_new pl210c_new pl210d_new pl210e_new pl210f_new pl210g_new pl210h_new pl210i_new pl210j_new pl210k_new pl210l_new"
egen workedmonths=anycount(`varlist_temp1new'), values(1 2 to 4)

egen unemployedmonths=anycount(`varlist_temp1new'), values(5)
egen retiredmonths=anycount(`varlist_temp1new'), values(6)
egen otherinactivemonths=anycount(`varlist_temp1new'), values(7 8 9)

generate totalmonths= workedmonths+ unemployedmonths+ retiredmonths+ otherinactivemonths
generate othermonths=12-totalmonths

list  workedmonths  pl210a pl210b pl210c pl210d pl210e pl210f pl210g pl210h pl210i pl210j pl210k pl210l in 1/20
tab age_group workedmonths if rx020<18|rx020>65

sort c_pid_n year, stable
tsset c_pid_n year 
generate workedmonthslead1 =F.workedmonths
generate workedmonths_change =D.workedmonths
generate workedmonths_changelead1 =F.workedmonths_change

***********************************************************************************************************
***********************************************************************************************************

/* Persons characteristics - Individual change from being unemployed 6 or less than 6 months to being unemployed more than 6 months */

generate unemployedmonthslead1 =F.unemployedmonths

/*generate unemployedmonths_change=0 if unemployedmonths==inlist(unemployedmonths,0,1,3,4,5,6)*/
generate unemployedmonths_change=0 
replace unemployedmonths_change=1 if unemployedmonthslead1>=7 & unemployedmonths<=6 & unemployedmonthslead1<. & unemployedmonths<.
label variable unemployedmonths_change  "New unemployed" 
label define  unemployedmonths_change 0 " (1) Not (2)" 1 " (2) Unemployed more than 6 months in t given unemployed 6 or less than 6 months in t-1" , replace
label  values unemployedmonths_change unemployedmonths_change

/* Persons characteristics - Individual change from being retired 6 or less than 6 months to being retired more than 6 months */

generate retiredmonthslead1 =F.retiredmonths

/*generate retiredmonths_change=0 if retiredmonths==inlist(retiredmonths,0,1,3,4,5,6)*/
generate retiredmonths_change=0 
replace retiredmonths_change=1 if retiredmonthslead1>=7 & retiredmonths<=6 &  retiredmonthslead1<. & retiredmonths<.
label variable retiredmonths_change  "New Retired" 
label define  retiredmonths_change 0 " (1) Not (2)" 1 " (2) Retired more than 6 months in t given retired 6 or less than 6 months in t-1" , replace
label  values retiredmonths_change retiredmonths_change

generate otherinactivemonthslead1 =F.otherinactivemonths

/* Persons characteristics - in t*/

generate newunemp_w=0 
replace newunemp_w=1 if unemployedmonths_change==1 & workedmonths>6
generate newunemp_r=0 
replace newunemp_r=1 if unemployedmonths_change==1 &  retiredmonths>6
generate newunemp_oi=0 
replace newunemp_oi=1 if unemployedmonths_change==1 &  otherinactivemonths>6

generate newretired_w=0 
replace newretired_w=1 if retiredmonths_change==1 & workedmonths>6
generate newretired_u=0 
replace newretired_u=1 if retiredmonths_change==1 &  unemployedmonths>6
generate newretired_oi=0 
replace newretired_oi=1 if retiredmonths_change==1 &  otherinactivemonths>6

 
generate work1=0
replace work1=1	if workedmonths_changelead1==0 	&  unemployedmonths_change==0 & retiredmonths_change==0
generate work2=0
replace work2=1 	if workedmonths_changelead1>0 & workedmonths_changelead1<. &  unemployedmonths_change==0 & retiredmonths_change==0
generate work3=0
replace work3=1	 if workedmonths_changelead1<0 		&  unemployedmonths_change==0 & retiredmonths_change==0
generate work4=0
replace work4=1	if unemployedmonths_change==1 & workedmonths>6

generate work14=0
replace work14=1	if  workedmonthslead1>6 & unemployedmonths>6 & workedmonthslead1<. & unemployedmonths<.

capture generate work15=0
replace work15=1	if  workedmonthslead1>6 & (otherinactivemonths>6|retiredmonths>6) & workedmonthslead1<. & otherinactivemonths<. & retiredmonths<.

generate work12=0
replace work12=1	if  workedmonthslead1>6 & workedmonths<=6 & workedmonthslead1<.

/*event: At least one Working after non-working*/
bysort y_c_hid: egen hid_work_after_nonwork0=total(work12) 
generate hid_work_after_nonwork1=0
replace hid_work_after_nonwork1=1 if hid_work_after_nonwork0>=1

/*event: At least one unemployed after working*/
bysort y_c_hid: egen hid_unemp_after_work0=total(work4) 
generate hid_unemp_after_work1=0
replace hid_unemp_after_work1=1 if hid_unemp_after_work0>=1

sort c_pid_n year, stable
tsset c_pid_n year 
generate pl170lead1=F.pl170
generate pl020lag1=L.pl020

generate work6=0
replace work6=1	if unemployedmonths_change==1 &  otherinactivemonths>6 &  otherinactivemonths<.

generate work18=0
replace work18=1	if unemployedmonths_change==1 &  (otherinactivemonths>6|retiredmonths>6) &  otherinactivemonths<. & retiredmonths<.

/*event: At least one unemployed after other inactive (none of them after working) */
bysort y_c_hid: egen hid_unemp_after_otherinactive0=total(work6) 
generate hid_unemp_after_otherinactive1=0
replace hid_unemp_after_otherinactive1=1 if hid_unemp_after_otherinactive0>=1 & hid_unemp_after_work1==0

generate work8=0
replace work8=1 	if retiredmonths_change==1 & workedmonths>6

/*event: At least one retired after working*/
bysort y_c_hid: egen hid_retired_after_work0=total(work8) 
generate hid_retired_after_work1=0
replace hid_retired_after_work1=1 if hid_retired_after_work0>=1
tab hid_retired_after_work1 if hh_head==1

generate work9=0
replace work9=1 	if retiredmonths_change==1 &  unemployedmonths>6

bysort y_c_hid: egen hid_retired_after_unemployed0=total(work9) 

generate work10=0
replace work10=1 if retiredmonths_change==1 &  otherinactivemonths>6

generate work13=0
replace work13=1	if  retiredmonths_change==1 & (unemployedmonths>6|otherinactivemonths>6)

capture generate work16=0
replace work16=1	if  (otherinactivemonthslead1>6|retiredmonthslead1>6) & workedmonths>6 & workedmonths<. & otherinactivemonthslead1<. & retiredmonthslead1<.

capture  generate work17=0
replace work17=1	if  (otherinactivemonthslead1>6|retiredmonthslead1>6) & unemployedmonths>6 & unemployedmonths<. & otherinactivemonthslead1<. & retiredmonthslead1<.

bysort y_c_hid: egen hid_retired_after_otherinactive0=total(work10) 

/*event: At least one retired and none new unemployed*/
generate hid_retired_after_notwork1=0
replace hid_retired_after_notwork1=1 if (hid_retired_after_unemployed0>=1 | hid_retired_after_otherinactive0>=1) & hid_retired_after_work1==0
/*& hid_unemp_after_work1==0 & hid_unemp_after_otherinactive1==0*/

label variable work1  "No change in the n∫ of worked months - no new retired and no new unemployed" 
label define  work1 0 "Not 1" 1 "No change in the n∫ of worked months - no new retired and no new unemployed ", replace
label variable work2  "Increase in the n∫ of worked months - no new retired and no new unemployed" 
label define  work2 0 "Not 1" 1 "Increase in the n∫ of worked months - no new retired and no new unemployed ", replace
label variable work3  "Decrease in the n∫ of worked months - no new retired and no new unemployed" 
label define  work3 0 "Not 1" 1 "Decrease in the n∫ of worked months - no new retired and no new unemployed ", replace

label variable work4  "Unemployed in t and Working in t-1" 
label define  work4 0 "Not 1" 1 "Unemployed in t and Working in t-1 ", replace
label variable work6  "Unemployed in t and other inactive in t-1" 
label define  work6 0 "Not 1" 1 "Unemployed in t and other inactive in t-1 ", replace

label variable work8  "Retired in t and working in t-1" 
label define  work8 0 "Not 1" 1 "Retired in t and working in t-1 ", replace
label variable work9  "Retired in t and unemployed in t-1" 
label define  work9 0 "Not 1" 1 "Retired in t and unemployed in t-1 ", replace
label variable work10  "Retired in t and other inactive in t-1" 
label define  work10 0 "Not 1" 1 "Retired in t and other inactive in t-1 ", replace

label variable hid_unemp_after_work1  "At least one unemployed in t after working in t-1" 
label define  hid_unemp_after_work1 0 "Not 1" 1 "At least one unemployed in t after working in t-1", replace

label variable hid_unemp_after_otherinactive1  "At least one unemployed in t after other inactive in t-1 and none unemployed after working" 
label define  hid_unemp_after_otherinactive1 0 "At least one unemployed in t after other inactive in t-1 and none unemployed after working", replace
/*(does not include hhs with at least one unemployed after working)*/

label variable hid_retired_after_work1  "At least one retired in t after working in t-1" 
label define  hid_retired_after_work1 0 "Not 1" 1 "At least one retired in t after working in t-1", replace
label variable hid_retired_after_notwork1  "At least one retired and none new unemployed or retired after working " 
label define  hid_retired_after_notwork1 0 "At least one retired and none new unemployed or retired after working", replace
/*in t or at least one retired in t after working in t-1(does not include hhs with at least one retire after working and with at least one unemplyoed)*/


/*Household characteristics - number of children*/
sort c_pid_n year, stable
tsset c_pid_n year
generate n_childslag1 = L.n_childs
generate n_childslead1 = F.n_childs
generate n_childrenchange=D.n_childs if n_childs<. & n_childslag1<.

generate n_adultslag1 =L.n_adults
generate n_adultslead1 =F.n_adults
generate n_adultschange=D.n_adults if n_adults<. & n_adultslag1<.

generate n_adults_children=n_adults+n_childs
generate n_adults_childrenlag1 =L.n_adults_children
generate n_adults_childrenlead1 =F.n_adults_children
generate n_adults_childrenchange=D.n_adults_children  if n_adults_children<. & n_adults_childrenlag1<.

generate n_adults_childrenchangelead1=F.n_adults_childrenchange

bysort y_c_hid: egen teste_0=count(pid) 
generate teste_1= n_adults+n_childs+birthb1+deads1

drop teste_0 teste_1

/*n∫ crianÁas sem nascimentos*/
/* crianÁas: n„o inclui aqueles cuja idade È -1 e 0*/
generate aux2=0 
replace aux2=1 if rx020 <14 & rx020>0 & year<=Year_out /*incluidos aqueles que Year_out=. */ /* n„o inclui rx020==-1*/

bysort y_c_hid: egen n_childswithoutbirths=sum(aux2) /* n„o inclui rx020==-1 e ==0*/
drop aux*

sort c_pid_n year, stable
tsset c_pid_n year
generate n_childswithoutbirthslag1= L.n_childswithoutbirths
generate n_childswithoutbirthslead1= F.n_childswithoutbirths
generate n_childrenchangewithoutbirths=D.n_childswithoutbirths if n_childswithoutbirths<. & n_childswithoutbirthslag1<.

generate n_adults_children_wobs=n_adults+n_childswithoutbirths
generate n_adults_children_wobslag1 =L.n_adults_children_wobs
generate n_adults_children_wobslead1 =F.n_adults_children_wobs
generate n_adults_children_wobschange=D.n_adults_children_wobs  if n_adults_children_wobs<. & n_adults_children_wobslag1<.

generate nadultschildrenwobschangelead1=F.n_adults_children_wobschange


bysort y_c_hid: egen teste_0=count(pid) 
generate teste_1= n_adults+n_childswithoutbirths+birth1+birthb1+deads1

drop teste_0 teste_1


bysort y_c_hid: egen HH14_13lag1_0=count(pid) if rx020==14 & rx020lag1==13  & year<=Year_out
bysort y_c_hid: egen HH14_13lag1_1=max(HH14_13lag1_0)
generate HH14_13lag1_2=HH14_13lag1_1
replace HH14_13lag1_2=0 if HH14_13lag1_1>=.
replace HH14_13lag1_2=1 if HH14_13lag1_1>0 & HH14_13lag1_1<.
label define HH14_13lag1_2 0 "HH without 14 at t and 13 at t-1" 1 "HH with 13 at t-1 and 14 at t" , replace

generate n_adults_children_change1=0
replace n_adults_children_change1=1	if n_adults_childrenchangelead1==0
generate n_adults_children_change2=0
replace n_adults_children_change2=1 	if n_adults_childrenchangelead1>0 	& n_adults_childrenchangelead1<. 
generate n_adults_children_change3=0
replace n_adults_children_change3=1	  if n_adults_childrenchangelead1<0 

label variable n_adults_children_change1  "No change in the n∫ of household members" 
label define  n_adults_children_change1 0 "Not 1" 1 "No change in the n∫ of remaining household members", replace
label variable n_adults_children_change2  "Increase in the n∫ of household members" 
label define  n_adults_children_change2 0 "Not 1" 1 "Increase in the n∫ of household members", replace
label variable n_adults_children_change3  "Decrease in the n∫ of household members" 
label define  n_adults_children_change3 0 "Not 1" 1 "Decrease in the n∫ of household members", replace

generate n_adults_childrenwobs_change1=0
replace n_adults_childrenwobs_change1=1	if nadultschildrenwobschangelead1==0
generate n_adults_childrenwobs_change2=0
replace n_adults_childrenwobs_change2=1 	if nadultschildrenwobschangelead1>0 	& nadultschildrenwobschangelead1<. 
generate n_adults_childrenwobs_change3=0
replace n_adults_childrenwobs_change3=1	  if nadultschildrenwobschangelead1<0 

label variable n_adults_childrenwobs_change1  "No change in the n∫ of household members" 
label define  n_adults_childrenwobs_change1 0 "Not 1" 1 "No change in the n∫ of remaining household members", replace
label variable n_adults_childrenwobs_change2  "Increase in the n∫ of household members" 
label define  n_adults_childrenwobs_change2 0 "Not 1" 1 "Increase in the n∫ of household members", replace
label variable n_adults_childrenwobs_change3  "Decrease in the n∫ of household members" 
label define  n_adults_childrenwobs_change3 0 "Not 1" 1 "Decrease in the n∫ of household members", replace


/*Household characteristics - Change in the N∫ persons in household -Equivalized*/
sort c_pid_n year, stable
tsset c_pid_n year
generate h_size_equivalizedlag1 =L.h_size_equivalized
generate h_size_equivalizedchange=D.h_size_equivalized if h_size_equivalized<. & h_size_equivalizedlag1<.
generate h_size_equivalizedchangelead1=F.h_size_equivalizedchange


generate h_size_equivalized_change1=0
replace h_size_equivalized_change1=1	if h_size_equivalizedchangelead1==0
generate h_size_equivalized_change2=0
replace h_size_equivalized_change2=1 	if h_size_equivalizedchangelead1>0 	& h_size_equivalizedchangelead1<. 
generate h_size_equivalized_change3=0
replace h_size_equivalized_change3=1	  if h_size_equivalizedchangelead1<0 

/* persons characteristics -  change in  worked months of other persons in household- unchange, increase or decrease in the number of months*/


bysort y_c_hid: egen hid_workedmonths0=total(workedmonths)  
generate  hid_workedmonths=hid_workedmonths0-workedmonths

bysort y_c_hid: egen hid_unemployedmonths0=total(unemployedmonths)  
bysort y_c_hid: egen hid_retiredmonths0=total(retiredmonths)  
bysort y_c_hid: egen hid_otherinactivemonths0=total(otherinactivemonths)  


generate hid_totalmonths0=hid_workedmonths0+hid_unemployedmonths0+hid_retiredmonths0+hid_otherinactivemonths0
generate hid_workedmonths0share=hid_workedmonths0/hid_totalmonths0
generate hid_unemployedmonths0share=hid_unemployedmonths0/hid_totalmonths0
generate hid_retiredmonths0share=hid_retiredmonths0/hid_totalmonths0
generate hid_otherinactivemonths0share=hid_otherinactivemonths0/hid_totalmonths0


sort c_pid_n year, stable
tsset c_pid_n year

generate hid_workedmonthslead1 =F.hid_workedmonths
generate hid_workedmonthschange =D.hid_workedmonths
generate hid_workedmonthschangelead1 =F.hid_workedmonthschange
generate hid_workedmonthslag1 =L.hid_workedmonths

generate hidwork1=0
replace hidwork1=1	if hid_workedmonthschangelead1==0 
generate hidwork2=0
replace hidwork2=1 	if hid_workedmonthschangelead1>0 	& hid_workedmonthschangelead1<. 
generate hidwork3=0
replace hidwork3=1	  if hid_workedmonthschangelead1<0

label variable hidwork1  "No change in the n∫ of worked months of remaining household members" 
label define  hidwork1 0 "Not 1" 1 "No change in the n∫ of worked months of remaining household members", replace
label variable hidwork2  "Increase in the n∫ of worked months of remaining household members" 
label define  hidwork2 0 "Not 1" 1 "Increase in the n∫ of worked months of remaining household members", replace
label variable hidwork3  "Decrease in the n∫ of worked months of remaining household members" 
label define  hidwork3 0 "Not 1" 1 "Decrease in the n∫ of worked months of remaining household members", replace


/*hid_workedmonths0 total hh members*/
sort c_pid_n year, stable
tsset c_pid_n year

generate hid_workedmonths0lead1 =F.hid_workedmonths0
generate hid_workedmonths0change =D.hid_workedmonths0
generate hid_workedmonths0changelead1 =F.hid_workedmonths0change
generate hid_workedmonths0lag1 =L.hid_workedmonths0

generate hid_workedmonths0changelead1_2=0
replace hid_workedmonths0changelead1_2=hid_workedmonths0changelead1 if hid_workedmonths0changelead1<.

generate hid_workedmonths0changepercent=hid_workedmonths0changelead1/hid_workedmonths0

generate hid_workmonths0changepercentexc=hid_workedmonths0changepercent if hid_unemp_after_work1==0 & hid_unemp_after_otherinactive1==0 & ///
hid_retired_after_work1==0 & hid_retired_after_notwork1==0

generate hidwork1_0=0
replace hidwork1_0=1	if hid_workedmonths0changelead1==0 
generate hidwork2_0=0
replace hidwork2_0=1 	if hid_workedmonths0changelead1>0 	& hid_workedmonths0changelead1<. 
generate hidwork3_0=0
replace hidwork3_0=1	  if hid_workedmonths0changelead1<0

label variable hidwork1_0  "No change in the n∫ of worked months of household members" 
label define  hidwork1_0 0 "Not 1" 1 "No change in the n∫ of worked months of household members", replace
label variable hidwork2_0  "Increase in the n∫ of worked months of household members" 
label define  hidwork2_0 0 "Not 1" 1 "Increase in the n∫ of worked months of household members", replace
label variable hidwork3_0  "Decrease in the n∫ of worked months of household members" 
label define  hidwork3_0 0 "Not 1" 1 "Decrease in the n∫ of worked months of household members", replace

bysort y_c_hid: egen hid_work14_0=count(pid) if work14==1

bysort y_c_hid: egen hid_work14_1=max(hid_work14_0)
replace hid_work14_1=0 if hid_work14_1>=.
generate hid_work14_2=0
replace hid_work14_2=1 if hid_work14_1==1

bysort y_c_hid: egen hid_work15_0=count(pid) if work15==1

bysort y_c_hid: egen hid_work15_1=max(hid_work15_0)
replace hid_work15_1=0 if hid_work15_1>=.
generate hid_work15_2=0
replace hid_work15_2=1 if hid_work15_1==1

bysort y_c_hid: egen hid_work4_0=count(pid) if work4==1

bysort y_c_hid: egen hid_work4_1=max(hid_work4_0)
replace hid_work4_1=0 if hid_work4_1>=.
generate hid_work4_2=0
replace hid_work4_2=1 if hid_work4_1==1

bysort y_c_hid: egen hid_work18_0=count(pid) if work18==1

bysort y_c_hid: egen hid_work18_1=max(hid_work18_0)
replace hid_work18_1=0 if hid_work18_1>=.
generate hid_work18_2=0
replace hid_work18_2=1 if hid_work18_1==1

bysort y_c_hid: egen hid_work16_0=count(pid) if work16==1

bysort y_c_hid: egen hid_work16_1=max(hid_work16_0)
replace hid_work16_1=0 if hid_work16_1>=.
generate hid_work16_2=0
replace hid_work16_2=1 if hid_work16_1==1


bysort y_c_hid: egen hid_work17_0=count(pid) if work17==1

bysort y_c_hid: egen hid_work17_1=max(hid_work17_0)
replace hid_work17_1=0 if hid_work17_1>=.
generate hid_work17_2=0
replace hid_work17_2=1 if hid_work17_1==1

generate hid_work_a=0
replace hid_work_a=hid_work14_2+hid_work15_2+hid_work4_2+hid_work18_2+hid_work16_2+hid_work17_2
tab hid_work_a
generate hid_work_b=0
replace hid_work_b=1 if hid_work_a==1
tab hid_work_b

generate work14a=work14*hid_work14_2*hid_work_b

generate work15a=work15*hid_work15_2*hid_work_b

generate work4a=work4*hid_work4_2*hid_work_b

generate work18a=work18*hid_work18_2*hid_work_b

generate work16a=work16*hid_work16_2*hid_work_b

generate work17a=work17*hid_work17_2*hid_work_b

set more off
generate amostra=1
bysort y_c_hid: egen amostraaux=max(hid_work_b)
replace amostra=0 if amostraaux==1 & (work14a==0 & work15a==0 & work4a==0 & work18a==0 & work16a==0 & work17a==0) & hid_work_b==1

sort c_pid_n year, stable
tsset c_pid_n year
generate General_healthlag1 = L.General_health
order c_pid_n year* General_health*
gen deltagenhealthtt_1=General_health-General_healthlag1
* two-year longitudinal sample
* income: non-missing and greater than zero
generate y_nmiss_2ys=0
replace  y_nmiss_2ys=1 if (General_health<. &  General_healthlag1<.)

sort country pid hid year

order y_nmiss_2ys
generate rb_t_t_1=.  if  y_nmiss_2ys==1
replace rb_t_t_1=rb062_2016 if  y_nmiss_2ys==1 & year==2016
replace rb_t_t_1=rb062_2015 if  y_nmiss_2ys==1 & year==2015
replace rb_t_t_1=rb062_2014 if  y_nmiss_2ys==1 & year==2014
replace rb_t_t_1=rb062_2013 if  y_nmiss_2ys==1 & year==2013
replace rb_t_t_1=rb062_2012 if  y_nmiss_2ys==1 & year==2012
replace rb_t_t_1=rb062_2011 if  y_nmiss_2ys==1 & year==2011
replace rb_t_t_1=rb062_2010 if  y_nmiss_2ys==1 & year==2010
replace rb_t_t_1=rb062_2009 if  y_nmiss_2ys==1 & year==2009
replace rb_t_t_1=rb062_2008 if  y_nmiss_2ys==1 & year==2008
replace rb_t_t_1=rb062_2007 if  y_nmiss_2ys==1 & year==2007
replace rb_t_t_1=rb062_2006 if  y_nmiss_2ys==1 & year==2006
replace rb_t_t_1=rb060_2005 if  y_nmiss_2ys==1 & year==2005

label var rb_t_t_1 "Two-year weights"

replace rb_t_t_1=rb062_2016 if  y_nmiss_2ys==1 & year==2015 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2015 if  y_nmiss_2ys==1 & year==2014 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2014 if  y_nmiss_2ys==1 & year==2013 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2013 if  y_nmiss_2ys==1 & year==2012 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2012 if  y_nmiss_2ys==1 & year==2011 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2011 if  y_nmiss_2ys==1 & year==2010 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2010 if  y_nmiss_2ys==1 & year==2009 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2009 if  y_nmiss_2ys==1 & year==2008 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2008 if  y_nmiss_2ys==1 & year==2007 & rb_t_t_1>=.
replace rb_t_t_1=rb062_2007 if  y_nmiss_2ys==1 & year==2006 & rb_t_t_1>=.
replace rb_t_t_1=rb062_2006 if  y_nmiss_2ys==1 & year==2005 & rb_t_t_1>=.
order c_pid_n year  Longitudinal_db country pid hid rb_t_t_1 rb062* 

sort country pid hid year

bysort year country_code hid: egen hid_rb_t_t_1=total(rb_t_t_1) if rb_t_t_1<.

drop y_nmiss_2ys

sort c_pid_n year, stable
tsset c_pid_n year
capture generate hidlead1=F.hid
generate rb_t_t_1lead1=F.rb_t_t_1
generate hid_rb_t_t_1lead1=F.hid_rb_t_t_1

bysort country_code hid: egen hid_rb_t_t_1b=mean(hid_rb_t_t_1) 
bysort country_code hid: egen hid_rb_t_t_1blead1=mean(hid_rb_t_t_1lead1) 

save "UDB_hrpd_2016_a_2005_tsset1.dta", replace

tab year

local ys "05 06 07 08 09 10 11 12 13 14 15 16"
foreach y of local ys  {
local lletter "D H P R"
foreach ll of local lletter  {
erase "SILC_`ll'_`y'.dta"
erase "UDB_l`y'`ll'_ver 20`y'.dta"
}
erase "UDB_id_transf_hrpd_20`y'.dta"
}
erase "UDB_hrpd_2005_2016.dta"


************************Part5 *******************************************************************************************
*************************************************************************************************************************
			
			
use "UDB_hrpd_2016_a_2005_tsset1.dta", clear

ren Longitudinal_db yearl
egen i=group(c_pid_n yearl)
xtset i year
bys i: gen nrep=_N
*
*Selecting observations
****
*loose 2005 because SILC started in 2003,2004,2005 - not possible to have 4 observations
tab nrep
*br if i==46
order i year yearl nrep

keep if nrep==4

drop nrep

bys i: gen nrep=_N

*sum hh030
*exit

ren General_health genhealth
cap label drop genhealthlabel
cap label define genhealthlabel 1 "Very good" 2 "Good" 3 "Fair" 4 "Bad" 5 "Very bad"
cap label values genhealth genhealthlabel

*ph010 - health * General_health
gen badhealth=1 if genhealth==4|genhealth==5
replace badhealth=0 if genhealth>=1&genhealth<=3
*
*replace age_r=floor(((rb060-rb080)*12+hb050-rb070)/12) if rb020=="IE"
*replace age_r=floor(((rb060-rb080)*12+hb050+6-rb070)/12) if rb020=="UK"
*UK AND IE do ot provide rb060 or rb070 for 4 year panel
*
gen ageg=1*(age_r>=18&age_r<=25)+2*(age_r>25&age_r<=35)+3*(age_r>35&age_r<=45)+ ///
 4*(age_r>45&age_r<=55)+5*(age_r>55&age_r<=65)+6*(age_r>65) 
replace ageg=. if age_r==.
tab rb020 ageg if rb020=="EL"&yearl==2009|rb020=="PO"&yearl==2009
tab rb020 ageg
bys i: gen nrepnew=_N
tab nrepnew nrep
sum badhealth 
drop if ageg==0
sum badhealth

cap label drop ageglabel
cap label define ageglabel 0 "unkown or less than 17" 1 "17-25" 2 "26-35" 3 "36-45" 4 "46-55" 5 "56-65" 6 "over 65"
cap label values ageg ageglabel

*Gender 1male 2 female
gen male=1*(rb090==1)+0*(rb090==2) 
replace male=. if rb090==.
gen married=(pb190==2)
replace married=. if pb190==.

*urbanisation (1 highest, 3 lowest)
ren db100 urbanisation
ren db040 region

*education
tab NivelEducacional year
*aqui 2014 igual a 2015 e 2016

gen educ6=0*(NivelEducacional==0&year<2014|NivelEducacional==0&year>2013)+ ///
1*(NivelEducacional==1&year<2014|NivelEducacional==1&year>2013|NivelEducacional==100&year>2013)+ ///
2*(NivelEducacional==2&year<2014|NivelEducacional>=200&NivelEducacional<300&year>2013)+ ///
3*(NivelEducacional==3&year<2014|NivelEducacional>=300&NivelEducacional<400&year>2013)+ ///
4*(NivelEducacional==4&year<2014|NivelEducacional>=400&NivelEducacional<500&year>2013)+ ///
5*(NivelEducacional==5&year<2014|NivelEducacional>=500&NivelEducacional<600&year>2013)+ ///
6*(NivelEducacional==6&year<2014|NivelEducacional>=600&NivelEducacional<900&year>2013)
replace educ6=. if NivelEducacional==.
cap label drop educ6label
cap label define educ6label 0 "pre-primary education" 1 "primary education" ///
2 "lower secondary education" 3 "upper secondary education" ///
4 "post-secondary non tertiary education" 5 "tertiary education" 
cap label values educ6 educ6label

tab educ6

*extra variables health economics paper: 

*1 employment PL031
*generate current_sc_status30=pl030 substituted by pl031 check note above 
generate emp_selfemp=pl031

/*
1 Employee working full-time
2 Employee working part-time
3 Self-employed working full-time (including family worker)
4 Self-employed working part-time (including family worker)
5 Unemployed
6 Pupil, student, further training, unpaid work experience
7 In retirement or in early retirement or has given up business
8 Permanently disabled or/and unfit to work
9 In compulsory military or community service
10 Fulfilling domestic tasks and care responsibilities
11 Other inactive person
*/
*employee or self-employed only with pl031
gen emp9=0
replace emp9=1 if pl031==1|pl031==3
replace emp9=2 if pl031==2|pl031==4
replace emp9=3 if pl031==5
replace emp9=4 if pl031==6
replace emp9=5 if pl031==7
replace emp9=6 if pl031==8
replace emp9=7 if pl031==9
replace emp9=8 if pl031==10
replace emp9=9 if pl031==11
tab emp9
replace emp9=pl030 if pl031==.
/*
replace emp=1 if pl031==.&pl030==1
replace emp=2 if pl031==.&pl030==2
replace emp=3 if pl031==.&pl030==3
replace emp=4 if pl031==.&pl030==4
replace emp=5 if pl031==.&pl030==5
replace emp=6 if pl031==.&pl030==6
replace emp=7 if pl031==.&pl030==7
replace emp=8 if pl031==.&pl030==8
replace emp=9 if pl031==.&pl030==9
replace emp=. if pl030==.&pl031==. 
*/
*pl030 1 can be 1 or 3 in pl031
*pl030 2 can be 2 or 4 in pl031
*pl030 3 (-9) is 5(-11) in pl031

cap label drop emp9label
cap label define emp9label 1 "Full-time" 2 "Part-time" ///
3 "Unemployed" 4 "Pupil, student, further training, unpaid work experience" ///
5 "In retirement or in early retirement or has given up business" ///
6 "Permanently disabled or/and unfit to work" ///
7 "In compulsory military or community service" ///
8 "Fulfilling domestic tasks and care responsibilities" ///
9 "Other inactive person" ///
. "missing information employment status"
cap label values emp9 emp9label
tab emp9

gen emp=.
replace emp=1 if pl031==1|pl031==3|pl031==2|pl031==4|pl030==1|pl030==2
replace emp=2 if pl031==5|pl030==3
replace emp=3 if pl031==6|pl031==10|pl031==9|pl030==4|pl030==7|pl030==8
replace emp=4 if pl031==7|pl031==8|pl031==11|pl030==5|pl030==6|pl030==9
tab emp
replace emp=. if pl030==.&pl031==. 

cap label drop emp4label
cap label define emp4label 1 "Full-time or Part-time" ///
2 "Unemployed" 3 "Pupil, student, further training, unpaid work experience, In compulsory military or community service. Fulfilling domestic tasks and care responsibilities" ///
4 "In retirement or in early retirement or has given up business  or Permanently disabled or/and unfit to work or Other inactive person" ///
. "missing information employment status"
cap label values emp emp4label
tab emp9 emp 

*drop emp==0 missing information 0.84%

*2 income
*lifetime income, lies current income,
*Total household income (gross and disposable)
*Gross income components at household level
*year esta bem pois não é o year do survey
*browse y_c_pid year pid hid rb110 rb120 rb140 year rb150 rb160 rb170 rb180 rb190 n_adults n_childs h_size_equivalized ///
*hy020_p2008_e hy020_p2008 if repeticoes==1
* indivÌduos com mais de uma famÌlia no mesmo ano: 
* para que possam ser acompanhados ao longo do tempo devemos ter apenas um registo para cada ano
* ie um rendimento equivalente para cada indivÌduo por ano: 
* fazer a mÈdia da informaÁ„o de cada famÌlia tendo em conta o tempo passado em cada uma
* analisar em Month_out Year_out N_Months_Inc_Per Month_in Year_in
*browse y_c_pid year pid hid rb110 rb120 Month_out Year_out N_Months_Inc_Per Month_in Year_in  rb170 n_adults n_childs h_size_equivalized ///
*hy020_p2008_e hy020_p2008 if repeticoes==1
* a informaÁ„o de Month_out e Year_out n„o parece bater certo com N_Months_Inc_Per, qual escolher?
*hy020_p2008_m HY010, HY020, HY022, HY023

*BETTER FROM FILE 4 - EQUIVALISED sum hy010_p2008_m hy020_p2008_m hy022_p2008_m hy023_p2008_m
*sum hy010_p2008_e hy020_p2008_e hy022_p2008_e hy023_p2008_e
*PY010G Gross employee cash or near cash income
*hy010_p2009_m total household gross income MORE INFORMATION 2ND BEST 
*HY010 = HY040G + HY050G + HY060G + HY070G + HY080G + HY090G + HY110G + [for all household members](PY010G + PY021G + PY050G + PY080G + PY090G + PY100G + PY110G + PY120G + PY130G + PY140G).
*In the case that Greece, Spain, France, Italy and Portugal cannot deliver a 
*gross income data component as from the first year of their operation, 
*the corresponding net income component shall be required.
*HY020: Total disposable household income MORE INFORMATION BEST
*HY022: Total disposable household income before social transfers other than old-age and survivor's benefits
*HY023: Total disposable household income before social transfers including old-age and survivor's benefits
*prices are in 2009, check the deflator database
*Difference with the EU-SILC Regulations: Accommodation provided for free or at a reduced rent by the employer to the employees as the main residence of the household, should be included in the non-cash employee income (PY020), and consequently not included under the variable imputed rent (HY030). When an employee occupies an employer-owned dwelling for free or at a reduced cost, the corresponding part of the imputed rent that is not paid by the employee should be recorded in employment income in kind (PY020) and not taken into account as part of the imputed rent (HY030).
*On the other hand, cash subsidies received from the employer for dwelling costs should be counted as employment income (PY010).
*The housing subsidies received from the government or from a non-profit institution should not be deducted from the imputed rent. These subsidies should be accounted for in housing allowances (HY070).
*HY030G/HY030N: Imputed rent
*HY054G: Family/Children-related allowances (Non-contributory and non means-tested) 
*HY064G: Social exclusion not elsewhere classified (Non-contributory and non means-tested) 
*HY074G: Housing allowances (Non-contributory and non means-tested)

cap sum py010g_p2008_e py010n_p2008_e 
cap sum py020g_p2008_e py020n_p2008_e 
cap sum py021g_p2008_e py021n_p2008_e
*BETTER FROM FILE 4 - EQUIVALISED sum hy010_p2008_m hy020_p2008_m hy022_p2008_m hy023_p2008_m

*sum py010g_p2008_m py010n_p2008_m 
*sum py020g_p2008_m py020n_p2008_m 
*sum py021g_p2008_m py021n_p2008_m
*sum py030g_p2008_m py031n_p2008_m
*PY010G/PY010N: Employee cash or near cash income BEST IF INDIVIDUAL
*PY020G/PY020N: Non-Cash employee income
*PY021G/PY021N: Company car
*PY030G: Employer's social insurance contribution
*PY031G: Optional employer's social insurance contributions

*PY080G/PY080N: Pension from individual private plans
*PY090G/PY090N: Unemployment benefits
*PY100G/PY100N: Old-age benefits
*PY110G/PY110N: Survivor' benefits
*PY120G/PY120N: Sickness benefits
*PY130G/PY130N: Disability benefits
*PY140G/PY140N: Education-related allowances

sum hy020_p2008*
cap gen tdhi=hy020_p2008_e
cap gen tdhi=hy020_p2008
ren *y*2008* *y*2009*

*new health economics 2017, check variables
*PH020: Suffer from any chronic (long-standing) illness or condition Domain/Area
*Health/ Long-standing health problem: Suffer from
*PH030: Limitation in activities because of health problems
*PH040: Unmet need for medical examination or treatment
*PH050: Main reason for unmet need for medical examination or treatment
*PH060: Unmet need for dental examination or treatment
*PH070: Main reason for unmet need for dental examination or treatment
*3 Tenure
*gen tenure_status_020=hh020
*gen tenure_status_021=hh021
ren Tenure_Status_020 hh020
ren Tenure_Status_021 hh021
*gen owner21=(hh021<=2) *WRONG miisiing goes to zero
gen owner=.
replace owner=1 if hh021>=1&hh021<=2
replace owner=0 if hh021>=3&hh021<=5
tab owner
tab owner hh021
*children
*This variable holds for the entire group of children aged between one year or more and below 16. If there are no children aged between 1-15 in the household, the flag "-2" should be used.
*hd100 -2 no children, 1 children
 
*weights rb064 each yearl, right? except 2006 and 2007
*weights 4 year panel
*looose yearl==2006, only 6612 so only DK, GR, NO
*so we are left with year 2004-2015
*The calculation of variables RB060s, RB063s and RB064s is described below. page 63/536 

local ys "2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016"
gen weight4=.
foreach y of local ys  {
cap replace weight4=rb064_`y' if year==`y'
} 
order *64* weight* i year yearl
*sum weight* rb_t_t_1 year yearl

foreach y of local ys  {
cap replace weight4=rb064_`y' if year==`y'
}
*sum weight* rb_t_t_1 year yearl

sort country pid hid year

generate y_nmiss_2ys=0
replace  y_nmiss_2ys=1 if (genhealth<. &  General_healthlag1<.)
count if  y_nmiss_2ys==1
sort country pid hid year

order y_nmiss_2ys
generate rb_t_t_4=.  if  y_nmiss_2ys==1
replace rb_t_t_4=rb064_2016 if  y_nmiss_2ys==1 & year==2016
replace rb_t_t_4=rb064_2015 if  y_nmiss_2ys==1 & year==2015 //if data upto 2016 then only needed up to 2014
replace rb_t_t_4=rb064_2014 if  y_nmiss_2ys==1 & year==2014 //if data upto 2016 then only needed up to 2014
replace rb_t_t_4=rb064_2013 if  y_nmiss_2ys==1 & year==2013
replace rb_t_t_4=rb064_2012 if  y_nmiss_2ys==1 & year==2012
replace rb_t_t_4=rb064_2011 if  y_nmiss_2ys==1 & year==2011
replace rb_t_t_4=rb064_2010 if  y_nmiss_2ys==1 & year==2010
replace rb_t_t_4=rb064_2009 if  y_nmiss_2ys==1 & year==2009
replace rb_t_t_4=rb064_2008 if  y_nmiss_2ys==1 & year==2008
replace rb_t_t_4=rb064_2007 if  y_nmiss_2ys==1 & year==2007
replace rb_t_t_4=rb060_2006 if  y_nmiss_2ys==1 & year==2006
replace rb_t_t_4=rb060_2005 if  y_nmiss_2ys==1 & year==2005

count if  y_nmiss_2ys==1 
count if  rb_t_t_1<. & y_nmiss_2ys==1
count if  rb_t_t_1>=. & y_nmiss_2ys==1

label var rb_t_t_4 "Four-year weights"
replace rb_t_t_1=rb062_2016 if  y_nmiss_2ys==1 & year==2015 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2015 if  y_nmiss_2ys==1 & year==2014 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2014 if  y_nmiss_2ys==1 & year==2013 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2013 if  y_nmiss_2ys==1 & year==2012 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2012 if  y_nmiss_2ys==1 & year==2011 & rb_t_t_1>=. //if data upto 2016 then only needed up to 2013 before 2014 - up to 2011
replace rb_t_t_1=rb062_2011 if  y_nmiss_2ys==1 & year==2010 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2010 if  y_nmiss_2ys==1 & year==2009 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2009 if  y_nmiss_2ys==1 & year==2008 & rb_t_t_1>=. 
replace rb_t_t_1=rb062_2008 if  y_nmiss_2ys==1 & year==2007 & rb_t_t_1>=.
replace rb_t_t_1=rb062_2007 if  y_nmiss_2ys==1 & year==2006 & rb_t_t_1>=.
replace rb_t_t_1=rb062_2006 if  y_nmiss_2ys==1 & year==2005 & rb_t_t_1>=.
order c_pid_n year yearl country pid hid rb_t_t_1 rb062* 

tab yearl year if  (rb_t_t_1>=.) & y_nmiss_2ys==1
tab yearl year if  (rb_t_t_1<.) & y_nmiss_2ys==1

tab country_code year if  (rb_t_t_1<.) & y_nmiss_2ys==1

**** 
*Selecting observations - end
****

keep i c_pid country rb020 yearl year yearl owner rb_t_t_1 weight4 tdhi ///
hy010_p2009_e hy020_p2009_e py010g_p2009_e py010n_p2009_e badhealth genhealth deltagenhealthtt_1 ///
age_r ageg male married urbanisation region owner married educ6 emp_selfemp emp n_childs n_adults_children 


cap label define childlabel 0 "0" 1 "1" 2 "2" 3 ">=3"
gen child=0*(n_childs==0)+1*(n_childs==1)+2*(n_childs==2)+3*(n_childs>=3)
cap label values child childlabel

cap label drop badhealthlabel
cap label define badhealthlabel 0 "Good health" 1 "Bad health" 
cap label values badhealth badhealthlabel
cap label drop malelabel
cap label define malelabel 0 "Female" 1 "Male" 
cap label values male malelabel
cap label drop marriedlabel
cap label define marriedlabel 0 "Not married" 1 "Married" 
cap label values married marriedlabel
cap label drop urbanisationlabel
cap label define urbanisationlabel 1 "Densely populated area" 2 "Intermediate area" 3 "Thinly area" 
cap label values urbanisation urbanisationlabel
la var ageg "Age"
la var married "Married" 
la var urbanisation "Urbanisation (1 highest, 3 lowest)"
la var educ6 "Education (1 lowest, 5 highest)"
la var emp "Employment status"
la var rb020 "Country"
la var tdhi "Total disp household equivalized income"
order i country year *health*
gen ltdhi=log(tdhi)
bys i: gen nrep=_N
keep if nrep==4
replace rb020="GR" if rb020=="EL"

*to include nl and pt (uk, fr,ee also have missing information about region)
replace urbanisation=1 if urbanisation==.&rb020=="PT"
replace urbanisation=1 if urbanisation==.&rb020=="NL"

ren genhealth health
keep if tdhi>0
keep i year rb020 country health emp child married ltdhi urbanisation male ageg educ6
order i year country rb020 health married child emp ltdhi urbanisation male ageg educ6
save "dataset.dta", replace
export delimited using "dataset.csv", replace
erase "UDB_hrpd_2016_a_2005_tsset1.dta"
erase "Deflators.dta"

